package com.icss.ah.storeApply.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.icss.j2ee.util.UUID;

/**
 * 店铺申请服务类
 * 1、店铺信息查询
 * 2、店铺申请信息保存：1>基础信息,2>经营信息,3>开户信息
 * 3、经营区域查询
 * 4、银行信息列表查询
 * 5、开户信息修改保存（单独保存需要审核，审核通过后存入店铺表）
 */
@Service
@Transactional
public class StoreApplyService {
	
	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	
	/**
	 * 店铺信息查询
	 * @date 2017-8-15
	 */
	public Map<String, Object> queryShopInfo(Map<String, String> map){
		Map<String, Object> obj = new HashMap<String, Object>();
		try {
			obj = sqlSessionTemplate.selectOne("StoreApplyBean.queryShopInfo",map);
			infolog.info("StoreApplyService|queryShopInfo|para:"+map+"|result:"+obj);
		} catch (Exception e) {
			errlog.error("StoreApplyService|queryShopInfo", e);
		} 
		return obj;
	}
	
	/**
	 * 省市县区查询
	 * @date 2017-8-15
	 */
	public List<Map<String, Object>> queryProvinceCityList(Map<String, String> map){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = sqlSessionTemplate.selectList("StoreApplyBean.queryProvinceCityList",map);
			infolog.info("StoreApplyService|queryBankList|list:"+list.size());
		} catch (Exception e) {
			errlog.error("StoreApplyService|queryBankList", e);
		} 
		return list;
	}
	
	/**
	 * 店铺申请基础信息保存
	 * @date 2017-8-15
	 */
	public String saveOne(Map<String, String> map){
		String result = "0, ";
		try {
			infolog.info("StoreApplyService|saveOne|map:"+map);
			String shopId = map.get("shopId");
			if(shopId == null || "".equals(shopId)){
				shopId = new UUID().toString();
				map.put("shopId", shopId);
				sqlSessionTemplate.insert("StoreApplyBean.saveOne",map);
				sqlSessionTemplate.update("StoreApplyBean.updateShopId",map);
			}else sqlSessionTemplate.update("StoreApplyBean.updateOne",map);
			result = "1,"+shopId;
		} catch (Exception e) {
			errlog.error("StoreApplyService|saveOne", e);
		}
		return result;
	}

	/**
	 * 经营区域树查询
	 * @date 2017-8-15
	 */
	public List<Map<String, Object>> queryCheckList(Map<String, String> map){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = sqlSessionTemplate.selectList("StoreApplyBean.queryCheckList",map);
			infolog.info("StoreApplyService|queryCheckList|para:"+map+"|list:"+list.size());
		} catch (Exception e) {
			errlog.error("StoreApplyService|queryCheckList", e);
		} 
		return list;
	}
	
	/**
	 * 店铺配送范围
	 * @param map
	 * @return
	 */
	public List<Map<String, Object>> queryDeliveryFee(Map<String, String> map){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = sqlSessionTemplate.selectList("StoreApplyBean.queryDeliveryFee",map);
			infolog.info("StoreApplyService|queryDeliveryFee|para:"+map+"|list:"+list.size());
		} catch (Exception e) {
			errlog.error("StoreApplyService|queryDeliveryFee", e);
		} 
		return list;
	}
	
	/**
	 * 店铺申请经营信息保存
	 * @date 2017-8-15
	 */
	public String saveTwo(Map<String, String> map,List<Map<String, String>> deliveryList,List<Map<String, String>> payList){
		String result = "保存失败";
		try {
			infolog.info("StoreApplyService|saveTwo|map:"+map);
			sqlSessionTemplate.update("StoreApplyBean.saveTwo",map);
			sqlSessionTemplate.delete("StoreApplyBean.delShopArea",map);
			String shopArea = map.get("shopArea");
			String[] areaArr = shopArea.split(",");
			List<Map<String, String>> list = new ArrayList<Map<String,String>>();
			for(String code : areaArr){
				Map<String, String> m = new HashMap<String, String>();
				m.put("uuid", new UUID().toString());
				m.put("areaCode", code);
				m.put("shopId", map.get("shopId"));
				m.put("corpId", map.get("corpId"));
				list.add(m);
			}
			sqlSessionTemplate.insert("StoreApplyBean.insertShopArea",list);
			sqlSessionTemplate.delete("StoreApplyBean.delDelivery",map);
			if(deliveryList!=null && deliveryList.size()>0) sqlSessionTemplate.insert("StoreApplyBean.insertDelivery",deliveryList);
			//sqlSessionTemplate.delete("StoreApplyBean.delPay",map);
			sqlSessionTemplate.update("StoreApplyBean.updatePay",map);
			if(payList!=null && payList.size()>0) sqlSessionTemplate.insert("StoreApplyBean.insertPay",payList);
			result = "保存成功";
		} catch (Exception e) {
			errlog.error("StoreApplyService|saveTwo", e);
		}
		return result;
	}
	
	/**
	 * 银行信息列表查询
	 * @date 2017-8-15
	 */
	public List<Map<String, Object>> queryBankList(){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = sqlSessionTemplate.selectList("StoreApplyBean.queryBankList");
			infolog.info("StoreApplyService|queryBankList|list:"+list.size());
		} catch (Exception e) {
			errlog.error("StoreApplyService|queryBankList", e);
		} 
		return list;
	}
	
	/**
	 * 店铺申请交易信息保存
	 * @date 2017-8-15
	 */
	public String saveThr(Map<String, String> map){
		String result = "保存失败";
		try {
			infolog.info("StoreApplyService|saveThr|map:"+map);
			sqlSessionTemplate.update("StoreApplyBean.saveThr",map);
			sqlSessionTemplate.update("StoreApplyBean.updateAccountStore",map);
			result = "保存成功";
		} catch (Exception e) {
			errlog.error("StoreApplyService|saveThr", e);
		}
		return result;
	}
	
	/**
	 * 开户信息修改保存
	 * @date 2017-8-15
	 *
	 */
	public String saveBank(Map<String, String> map){
		String result = "保存失败";
		try {
			infolog.info("StoreApplyService|saveThr|map:"+map);
			map.put("flag", "1");
			Map<String, Object> obj = sqlSessionTemplate.selectOne("StoreApplyBean.getBankInfo",map);
			String uuid = obj == null ? "" : (String) obj.get("UUID");
			if(null==uuid||"".equals(uuid)){
				map.put("uuid", new UUID().toString());
				sqlSessionTemplate.insert("StoreApplyBean.saveBank",map);
			}else{
				map.put("uuid", uuid);
				sqlSessionTemplate.update("StoreApplyBean.updateBank",map);
			}
			result = "保存成功";
		} catch (Exception e) {
			errlog.error("StoreApplyService|saveThr", e);
		}
		return result;
	}
	
	/**
	 * 开户信息查询
	 * @date 2017-8-15
	 */
	public Map<String, Object> getBankInfo(Map<String, String> map){
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			infolog.info("StoreApplyService|getBankInfo|map:"+map);
			result = sqlSessionTemplate.selectOne("StoreApplyBean.getBankInfo",map);
		} catch (Exception e) {
			errlog.error("StoreApplyService|getBankInfo", e);
		}
		return result;
	}

}
